/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#pragma once

#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkImageActor.h"
#include "vtkRenderWindow.h"
#include "itkImageToVTKImageFilter.h"
#include "GelImage.h"
#include "GelImageViewer.h"
#include "vtkImageShiftScale.h"
#include "GelCorrelationAnalysis.h"
#include "vtkCallbackCommand.h"
#include "vtkScalarBarActor.h"
#include "vtkLookupTable.h"
#include "vtkColorTransferFunction.h"
#include "vtkInteractorStyleRubberBand2D.h"
#include "vtkWorldPointPicker.h"
#include "vtkProperty2D.h"
#include "vtkImageMapToColors.h"
#include "vtkPropPicker.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleUser.h"
#include "vtkJPEGWriter.h"
#include "vtkWindowToImageFilter.h"

#include "vtkTextProperty.h"
#include "vtkTextActor.h"
#include "vtkScalarBarWidget.h"
#include "File.h"
#include "vtkImageFlip.h"
#include "vtkTextMapper.h"



//todo: higher class should synchronize cameras.

class GelImageViewer;

class ResultImageViewer
{
public:
	typedef itk::ImageToVTKImageFilter<GelCorrelationAnalysis::DataImageType> ConnectorType;

	ResultImageViewer(vtkRenderWindowInteractor* aRenWinInter, char* aPath, GelImageViewer* aGelImgViewer, vtkColorTransferFunction * ctf);
	~ResultImageViewer(void);
	void Update();
	void UpdateConnectors();
	void Clear();
	void Render();
	void DisplayElement(int);
	void UpdateCamera();
	void Load(gel2de::File* aDirectory, std::string aIdString);
	void Load();
	void SetMetaInfo(std::string aParameterName, int aNumberOfGels, double aAverage);
	void HideRectangle();
	void UpdateRectangle(vtkPoints* aPoints);
	void SetRectangleVisible(int aVisible);
	int HasResult();
	void SetRoi(int startx, int starty, int startz, int stopx, int stopy, int stopz);


  void SetColorTransferFunction(vtkColorTransferFunction *ctf);
  void SetGelCorrelationAnalysis(GelCorrelationAnalysis* aGelCorrelationAnalysis);
  void Event(unsigned long event);

  void AnalyzeROI();
  void GetROIDefinition(GelCorrelationAnalysis::DataImageType::RegionType* aRegionType);
  void ScreenDump();

  
private:
	void ConstructVTK();

	void InitializeRectangle();
	void ListenToGelImageInteractor();
	void SetupMouseListener();

	vtkRenderWindowInteractor* m_renderWindowInteractor;
	GelImageViewer* m_gelImageViewer;
	vtkRenderer* m_renderer;
	vtkImageActor* m_actor;
	vtkTextActor* m_cursorTextActor;
	vtkTextActor* m_ROITextActor;
	vtkTextActor* m_infoTextActor;
	vtkTextMapper* m_textMapper;
	ConnectorType::Pointer m_Cconnector;
	ConnectorType::Pointer m_Sconnector;
	ConnectorType::Pointer m_Pconnector;
	ConnectorType::Pointer m_PTconnector;
	ConnectorType::Pointer m_Dconnector;
	ConnectorType::Pointer m_CSconnector;
	ConnectorType::Pointer m_CSDconnector;
	vtkScalarBarActor *m_colorBar;
	vtkImageMapToColors *m_imageMapToColors;
	char* m_path;
	GelCorrelationAnalysis* m_gelCorrelationAnalysis;
	GelCorrelationAnalysis::RGBReaderType::Pointer m_reader;
	std::string m_baseFileName;

	vtkPropPicker* m_pointPicker;
	
	vtkImageFlip* m_flipper;

	vtkColorTransferFunction * m_ctf;

	double m_buttonDownPosition[3];
	double m_currentPos[3];

	BOOL m_buttonDown;

	vtkActor* m_rectangleActor;
	vtkPoints* m_rect;

	double m_maxCorr;
	double m_maxAntiCorr;

  
};
